<template>
  <el-drawer title="数据表" v-model="tableVisible" close-on-click-modal size="40%">
    <el-form :model="state.queryForm" @keyup.enter="getDataList" ref="queryRef">
      <el-row class="mb-2" :gutter="20">
        <el-col :span="8">
          <el-form-item label="数据源" prop="name">
            <el-select @change="getDataList" placeholder="请选择数据源"
                       style="max-width: 120px"
                       v-model="state.queryForm.dsName">
              <el-option label="默认数据源" value="master"></el-option>
              <el-option :key="ds.id" :label="ds.name" :value="ds.name" v-for="ds in datasourceList"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="$t('table.tableName')" prop="tableName">
            <el-input :placeholder="$t('table.inputtableNameTip')+$t('table.inputtableCommentTip')" style="max-width: 120px"
                      v-model="state.queryForm.tableName"/>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item>
            <el-button @click="getDataList" icon="search" type="primary">
              {{ $t('common.queryBtn') }}
            </el-button>
            <el-button icon="Refresh" @click="resetQuery">{{ $t('common.resetBtn') }}</el-button>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>

    <el-row class="mb-2">
      已选表:
      <el-tag
          class="ml-2"
          v-for="tag in selectObjs"
          :key="tag"
          closable
          @close="handleClose(tag)"
          :disable-transitions="false"
      >
        {{ tag }}
      </el-tag>
    </el-row>
    <!-- Sidebar Content -->
    <el-row>
      <el-table
          ref="tableRef"
          :data="state.dataList"
          style="width: 100%"
          v-loading="state.loading"
          border
          row-key="name"
          width="30%"
          @selection-change="handleSelectionChange"
          :cell-style="tableStyle.cellStyle"
          :header-cell-style="tableStyle.headerCellStyle"
      >
        <el-table-column type="selection" :reserve-selection="true" width="40"/>
        <el-table-column :label="t('table.index')" type="index" width="60"/>
        <el-table-column :label="t('table.tableName')" prop="name" show-overflow-tooltip/>
        <el-table-column :label="t('table.tableDesc')" prop="comment" show-overflow-tooltip/>
      </el-table>
      <pagination @current-change="currentChangeHandle" @size-change="sizeChangeHandle"
                  v-bind="state.pagination"/>
    </el-row>
  </el-drawer>
</template>
<script setup lang="ts" name="AiTablesDialog">
import {list} from "/@/api/gen/datasource";
import {fetchList} from '/@/api/gen/table';
import {BasicTableProps, useTable} from "/@/hooks/table";
import {useI18n} from "vue-i18n";
import {useMessage} from "/@/hooks/message";

const emit = defineEmits(['refresh']);
const tableVisible = ref(false)
const tableList = ref([])
const queryString = ref('')
const datasourceList = ref();
const {t} = useI18n();
const queryRef = ref()
const tableRef = ref()
// 多选rows
const selectObjs = ref([]) as any;
// 是否可以多选
const multiple = ref(true);

const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: {
    dsName: 'master',
  },
  pageList: fetchList
});

//  table hook
const {getDataList, currentChangeHandle, sizeChangeHandle, downBlobFile, tableStyle} = useTable(state);


/**
 * 查询所有数据源.
 */
const querySearchAsync = async () => {
  const {data} = await list()
  datasourceList.value = data
  tableList.value = data
  queryString.value = datasourceList.value[0].name
}

// 多选事件
const handleSelectionChange = (objs: { name: string }[]) => {
  if (objs.length > 5) {
    useMessage().warning('最多选择5个关联表');
    return
  }
  selectObjs.value = objs.map(({name}) => name);
  multiple.value = !objs.length;
  // Emit a 'refresh' event with the selected table
  emit('refresh', {dsName: state.queryForm.dsName, tableNames: selectObjs.value});
};

// 清空搜索条件
const resetQuery = () => {
  selectObjs.value = [];
  tableRef.value?.clearSelection();
  queryRef.value?.resetFields();
  state.queryForm.deptId = '';
  getDataList();
};

// 关闭处理
const handleClose = (tag: string) => {
  selectObjs.value = selectObjs.value.filter(item => item !== tag);
}

/**
 * 打开提示词选择界面.
 */
const openDialog = () => {
  querySearchAsync()
  tableVisible.value = true
}

// Expose the openDialog function
defineExpose({
  openDialog
});
</script>
